﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Data;
using System.Windows.Documents;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Imaging;
using System.Windows.Shapes;
using ClipperBaseUI.Controls;
using Clipper.BLL;

namespace Clipper.DataManage
{
    /// <summary>
    /// WinReagentInput.xaml 的交互逻辑
    /// </summary>
    public partial class WinReagentInput : BaseWindow
    {
        public WinReagentInput()
        {
            InitializeComponent();
        }
        Model.Reagent mModel = null;
        private void BaseWindow_Loaded(object sender, RoutedEventArgs e)
        {
            BLL.Reagent reagent = new Reagent(); 
            BLL.DetectItems detectItems = new BLL.DetectItems();
            BLL.ReagentState reagentStateBll=new ReagentState();
            reagentOrder.Items.Add("1");
            reagentOrder.Items.Add("2");
            reagentOrder.Items.Add("3");
            
            DetectItems.ItemsSource = detectItems.GetModelList("");
            cboState.ItemsSource = reagentStateBll.GetModelList("");
            if (BaseServiceProvider.InputName == Model.InputEnum.Update)
            {
                mModel = BaseServiceProvider.DataModel as Model.Reagent;
                if (mModel != null)
                {
                    reagentID.IsEnabled = false;
                    
                    DataContext = mModel;
                }
            }
        }

        private void Save_Click(object sender, RoutedEventArgs e)
        {
            bool result = true;
            if (Check())
            {
                Clipper.Model.Reagent model = new Clipper.Model.Reagent();
                Clipper.BLL.Reagent bll = new BLL.Reagent();
                //model.reagentID = Convert.ToInt32(reagentID.Text);
                if (mModel != null)
                {
                    model.reagentID = mModel.reagentID;
                }
                model.brand = brand.Text;
                model.DetectItem = Convert.ToString(DetectItems.SelectedValue);
                model.batchNumber = batchNumber.Text;
                model.barcode = barcode.Text;
                model.reagentOrder = Convert.ToInt32(reagentOrder.Text);
                model.peirodValidity = Convert.ToDateTime(peirodValidity.Text);
                model.ReagentLeft = ntxtReagentLeft.Number;
                model.StateId = Convert.ToInt32(cboState.SelectedValue);
                if (BaseServiceProvider.InputName == Model.InputEnum.Add)
                {
                    result = bll.Add(model);
                }
                else
                {
                    result = bll.Update(model);
                }
                
                this.DialogResult = result;
                
            }
        }

        private void Cancel_Click(object sender, RoutedEventArgs e)
        {
            this.DialogResult = null;
            Close();
        }
        private bool Check()
        {
            List<string> strErr = new List<string>();

            //if (string.IsNullOrEmpty(reagentID.Text))
            //{
            //    strErr.Add("ReagentIDNotNullTip");
            //}

            if (DetectItems.SelectedIndex < 0 )
            {
                strErr.Add("DetectItemsNotNullTip");
            }

            if (string.IsNullOrEmpty(brand.Text))
            {
                strErr.Add("BrandNotNullTip");
            }

            if (string.IsNullOrEmpty(batchNumber.Text))
            {
                strErr.Add("BarCodeNotNullTip");
            }

            if (string.IsNullOrEmpty(barcode.Text))
            {
                strErr.Add("BarCodeNotNullTip");
            }

            if (string.IsNullOrEmpty(reagentOrder.Text))
            {
                strErr.Add("ReagentOrderNotNullTip");
            }

            if (string.IsNullOrEmpty(peirodValidity.Text))
            {
                strErr.Add("PeirodValidityNotNullTip");
            }
            if (cboState.SelectedIndex<0)
            {
                strErr.Add("ReagentStateNotNullTip");
            }
            if (strErr.Count>0)
            {
                UIHelper.ShowKeyMessage(strErr);
                return false;
            }
            return true;
        }

    }
}